
public class Game 
{
	private String rcd;
	private AI ai = new AI();
	
	/**rcd setter
	 * @param "row","col","diag1" or "diag2"**/
	public void setRcd(String rcd)
	{
		this.rcd = rcd;
	}

	/**rcd getter
	 * @return "row","col","diag1" or "diag2"**/
	public String getRcd() 
	{
		return rcd;
	}
	
	/**class constructor**/
	public Game(){}
	
	/**game start
	 * @param the number of the column**/
	public void start(int c)
	{
		int m = GameWindowAWT.getBoard().getMode();
		if (m == 1)
			singlePlayer(c);
		else
			multiPlayer(c);
	}
	
	/**single player game mode
	 * @param the number of the column**/
	public void singlePlayer(int c)
	{		
		if (GameWindowAWT.getBoard().getPlayer() == 1)
		{
			int i=10;
			while ((GameWindowAWT.getBoard().getHoles()[i][c].getIs() != 0)&&(i>0))
				i --;
			GameWindowAWT.getBoard().getHoles()[i][c].setIs(1);
			GameWindowAWT.getBoard().getHoles()[i][c].setImgstr("res\\coinred.png");
			GameWindowAWT.getBoard().getHoles()[i][c].repaint();
			if (detectWinner(c,i,4,GameWindowAWT.getBoard().getPlayer()) == true)
			{
				OptionsPanelAWT.text.setText("We have a winner!! Player 1");
				GameWindowAWT.getBoard().setPlayer(0);
			}
			else 
			{
				int x[] = new int[2];
				x[0] = i;
				x[1] = c;
				ai.setPlayerlastmove(x);
				ai.place();
				//GameWindowAWT.getBoard().setPlayer(2);
				//ai.place();
			}
		}
		else 
			if (GameWindowAWT.getBoard().getPlayer() == 2) 
			{
				int i=10;
				while ((GameWindowAWT.getBoard().getHoles()[i][c].getIs() != 0)&&(i>0))
					i --;
				GameWindowAWT.getBoard().getHoles()[i][c].setIs(2);
				GameWindowAWT.getBoard().getHoles()[i][c].setImgstr("res\\coingreen.png");
				GameWindowAWT.getBoard().getHoles()[i][c].repaint();
				if (detectWinner(c,i,4,GameWindowAWT.getBoard().getPlayer()) == true)
				{
					OptionsPanelAWT.text.setText("We have a winner!! Player 2");
					GameWindowAWT.getBoard().setPlayer(0);
				}
				else
				{
					int x[] = new int[2];
					x[0] = i;
					x[1] = c;
					ai.setAilastmove(x);
					GameWindowAWT.getBoard().setPlayer(1);
				}
			}
	}
	
	/**multi player game mode
	 * @param the number of the column**/
	public void multiPlayer(int c)
	{		
		if (GameWindowAWT.getBoard().getPlayer() == 1)
		{
			int i=10;
			while ((GameWindowAWT.getBoard().getHoles()[i][c].getIs() != 0)&&(i>0))
				i --;
			GameWindowAWT.getBoard().getHoles()[i][c].setIs(1);
			GameWindowAWT.getBoard().getHoles()[i][c].setImgstr("res\\coinred.png");
			GameWindowAWT.getBoard().getHoles()[i][c].repaint();
			if (detectWinner(c,i,4,GameWindowAWT.getBoard().getPlayer()) == true)
			{
				//System.out.println(getRcd());
				OptionsPanelAWT.text.setText("We have a winner!! Player 1");
				GameWindowAWT.getBoard().setPlayer(0);
			}
			else 
			{
//				int x[] = new int[2];
//				x[0] = i;
//				x[1] = c;
				//ai.setPlayerlastmove(x);
				GameWindowAWT.getBoard().setPlayer(2);
				//ai.canPlayerWin();
				//ai.place();
			}
		}
		else 
			if (GameWindowAWT.getBoard().getPlayer() == 2) 
			{
				int i=10;
				while ((GameWindowAWT.getBoard().getHoles()[i][c].getIs() != 0)&&(i>0))
					i --;
				GameWindowAWT.getBoard().getHoles()[i][c].setIs(2);
				GameWindowAWT.getBoard().getHoles()[i][c].setImgstr("res\\coingreen.png");
				GameWindowAWT.getBoard().getHoles()[i][c].repaint();
				if (detectWinner(c,i,4,GameWindowAWT.getBoard().getPlayer()) == true)
				{
					//System.out.println(getRcd());
					OptionsPanelAWT.text.setText("We have a winner!! Player 2");
					GameWindowAWT.getBoard().setPlayer(0);
				}
				else
				{
//					int x[] = new int[2];
//					x[0] = i;
//					x[1] = c;
					GameWindowAWT.getBoard().setPlayer(1);
				}
			}
	}

	/**places the coin of the ai
	 * @param the number of the column**/
	public void placeCoin(int column)
	{
		int i=10;
		while ((GameWindowAWT.getBoard().getHoles()[i][column].getIs() != 0)&&(i>0))
			i --;
		GameWindowAWT.getBoard().getHoles()[i][column].setIs(2);
		GameWindowAWT.getBoard().getHoles()[i][column].setImgstr("res\\coingreen.png");
		GameWindowAWT.getBoard().getHoles()[i][column].repaint();
		if (detectWinner(column,i,4,GameWindowAWT.getBoard().getPlayer()) == true)
		{
			OptionsPanelAWT.text.setText("We have a winner!! Player 2");
			GameWindowAWT.getBoard().setPlayer(0);
		}
		else
		{
			int x[] = new int[2];
			x[0] = i;
			x[1] = column;
			GameWindowAWT.getBoard().setPlayer(1);
		}
	}

	
	/**detects winner
	 * @return true if anyone won, false otherwise
	 * @param poscol  the number of the column
	 * @param posrow  the number of the row
	 * @param n the number of coins required to win
	 * @param pl player id**/
	public boolean detectWinner(int poscol,int posrow, int n, int pl)
	{
		int lr=0,rr=0,lg=0,rg=0,tr=0,tg=0;
		boolean w = false;
		
/*******************************************************************************
* 								piros jatekos								   *
********************************************************************************/
		
		if (pl == 1)  
		{
			int i=poscol;
			//lr = 1;
			while (GameWindowAWT.getBoard().getHoles()[posrow][i].getIs() == 1)//&&(i > 1)) //balra
			{
				lr++;
				i--;
			}			
			i = poscol+1;
			while (GameWindowAWT.getBoard().getHoles()[posrow][i].getIs() == 1)//&&(i < 10)) //jobbra
			{
				rr++;
				i++;
			}
			tr = lr+rr;
			if (tr >= n)
			{
				setRcd("row");
				w =  true;
			}

/**************************************************************************
*                  ha nincs nyero sor, vizsgalom az oszlopon			  *
***************************************************************************/
			
			if (w == false)
			{
				tr = 0;
				i = posrow;
				while (GameWindowAWT.getBoard().getHoles()[i][poscol].getIs() == 1)//&&(i < 10)) //lefele
				{
					i++;
					tr++;
				}
				if (tr >= n)
				{
					setRcd("col");
					w =  true;
				}
			}

/**************************************************************************
*                  ha nincs nyero oszlop, vizsgalom az atlokon			  *
**************************************************************************/

			if (w == false) //foatlo
			{
				lr = 0;
				rr = 0;
				i = posrow;
				int j = poscol;
				while (GameWindowAWT.getBoard().getHoles()[i][j].getIs() == 1) //balra fel
				{
					lr++;
					i--;
					j--;
				}
				i = posrow+1;
				j = poscol+1;
				while (GameWindowAWT.getBoard().getHoles()[i][j].getIs() == 1) //jobbra le
				{
					rr++;
					i++;
					j++;
				}
				tr = lr+rr;
				if (tr >= n)
				{
					setRcd("diag1");
					w = true;
				}
				else //mellekatlo
				{
					lr = 0;
					rr = 0;
					tr = 0;
					i = posrow;
					j = poscol;
					while (GameWindowAWT.getBoard().getHoles()[i][j].getIs() == 1) //jobbra fel
					{
						rr++;
						i--;
						j++;
					}
					lr = 1;
					rr = 0;
					i = posrow+1;
					j = poscol-1;
					while (GameWindowAWT.getBoard().getHoles()[i][j].getIs() == 1) //balra le
					{
						lr++;
						i++;
						j--;
					}
					tr = rr+lr;
					if (tr >= n)
					{
						setRcd("diag2");
						w = true;
					}
				}
			}		
		}

/*******************************************************************************
 * 								zold jatekos								   *
 *******************************************************************************/
		
		else
		{
			int i=poscol;
			//lg = 1;
			while (GameWindowAWT.getBoard().getHoles()[posrow][i].getIs() == 2)//&&(i > 1)) //balra
			{
				lg++;
				i--;
			}
			i = poscol+1;
			while (GameWindowAWT.getBoard().getHoles()[posrow][i].getIs() == 2)//&&(i < 10)) //jobbra
			{
				rg++;
				i++;
			}
			tg = lg+rg;
			if (tg >= n)
			{
				setRcd("row");
				w =  true;
			}
			
/**************************************************************************
*                  ha nincs nyero sor, vizsgalom az oszlopon			  *
***************************************************************************/
			
			if (w == false)
			{
				tg = 0;
				i = posrow;
				while (GameWindowAWT.getBoard().getHoles()[i][poscol].getIs() == 2)//&&(i < 10)) //lefele
				{
					i++;
					tg++;
				}
				if (tg >= n)
				{
					setRcd("col");
					w =  true;
				}
			}
			
/**************************************************************************
*                  ha nincs nyero oszlop, vizsgalom az atlokon			  *
**************************************************************************/
			
			if (w == false) //foatlo
			{
				lg = 0;
				rg = 0;
				i = posrow;
				int j = poscol;
				while (GameWindowAWT.getBoard().getHoles()[i][j].getIs() == 2) //balra fel
				{
					lg++;
					i--;
					j--;
				}
				i = posrow+1;
				j = poscol+1;
				while (GameWindowAWT.getBoard().getHoles()[i][j].getIs() == 2) //jobbra le
				{
					rg++;
					i++;
					j++;
				}
				tg = lg+rg;
				if (tg >= n)
				{
					setRcd("diag1");
					w = true;
				}
				else //mellekatlo
				{
					lg = 0;
					rg = 0;
					tg = 0;
					i = posrow;
					j = poscol;
					while (GameWindowAWT.getBoard().getHoles()[i][j].getIs() == 2) //jobbra fel
					{
						rg++;
						i--;
						j++;
					}
					lg = 1;
					rg = 0;
					i = posrow+1;
					j = poscol-1;
					while (GameWindowAWT.getBoard().getHoles()[i][j].getIs() == 2) //balra le
					{
						lg++;
						i++;
						j--;
					}
					tg = rg+lg;
					if (tg >= n)
					{
						setRcd("diag2");
						w = true;
					}
				}
			}		
		}
		return w;
	}
}
